<template>
  <div class="app-container">
    <el-radio-group v-model="queryParams.deviceType" @change="handleQuery" style="margin-bottom: 30px;">
      <el-radio-button :label="0">地磁设备</el-radio-button>
      <el-radio-button :label="4">发光二维码</el-radio-button>
    </el-radio-group>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="卡商公司" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入卡商公司" />
      </el-form-item>
      <el-form-item label="卡片ID" prop="cardId">
        <el-input v-model="queryParams.cardId" placeholder="请输入卡片ID" />
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input v-model="queryParams.deviceId" placeholder="请输入设备ID" />
      </el-form-item>
      <el-form-item label="卡片状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="选择卡片状态">
          <el-option
            v-for="dict in dict.type.card_decive_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
        v-hasPermi="['system:NBCardDeviceDetail:import']">导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="卡商公司" align="center" prop="name" />
      <el-table-column label="批次" align="center" prop="batch" />
      <el-table-column label="卡片ID" align="center" prop="cardId" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="归属(项目)" align="center" prop="projectNo" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="{row}">
           <dict-tag
           :options="dict.type.card_decive_status"
           :value="row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="operator"></el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        name="excelfile"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入结果对话框 -->
    <el-dialog :title="uploadMsg.title" :visible.sync="uploadMsg.open" width="500px" append-to-body>
      <div class="diy-mb-3">{{ uploadMsg.msg }}</div>
      <div v-for="(item,index) in uploadMsg.error" :key="index">{{ item }}</div>
    </el-dialog>
  </div>
</template>

<script>
import { listNBCardDeviceDetail  } from "@/api/deviceNBCardManag/cardDevicemanage";
import { validateMobile } from '@/utils/validate'
import {getToken} from "@/utils/auth";
export default {
  name: "CardDevicemanage",
  dicts: ['card_decive_status'],
  components:{},
  data() {
    return {
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/NBCardDeviceDetail/importExcel"
      },
      //导入信息参数
      uploadMsg: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入结果",
        error: [],//错误信息
        msg: "", //提示信息
      },
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电桩表格数据
      postList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name:null,
        cardId:null,
        deviceId:null,
        status:null,
        deviceType:0,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      listNBCardDeviceDetail(queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = `卡设备数据导入`;
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/NBCardDeviceDetail/getImportTemplate', {}, `卡设备导入模版${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (response.code === 200) {
        this.uploadMsg.open = true;
        this.uploadMsg.error = response.data.error;
        this.uploadMsg.msg = response.data.msg
        this.getList();
      } else {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      }
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
